package com.yfqy.admin.domain.vo.common;

import com.yfqy.admin.enums.ResponseEnum;
import lombok.Getter;
import lombok.Setter;

import java.io.Serial;
import java.io.Serializable;

@Setter
@Getter
public class ResultVO<T> implements Serializable {

    @Serial
    private static final long serialVersionUID = -6149948941889889657L;
    // Getter 和 Setter 方法
    private Integer code;
    private String message;
    private T data;

    public static final String SUCCESS_CODE = "success";
    public static final String FAIL_CODE = "fail";

    // 无数据成功响应
    public static <T> ResultVO<T> success() {
        return ResultVOBuilder.<T>aResult()
                .data(null)
                .code(0)
                .msg(SUCCESS_CODE)
                .build();
    }

    // 带消息的成功响应
    public static <T> ResultVO<T> success(String message) {
        return ResultVOBuilder.<T>aResult()
                .data(null)
                .code(0)
                .msg(message)
                .build();
    }

    // 带数据的成功响应
    public static <T> ResultVO<T> data(T data) {
        return ResultVOBuilder.<T>aResult()
                .data(data)
                .code(0)
                .msg(SUCCESS_CODE)
                .build();
    }

    // 带数据和消息的成功响应
    public static <T> ResultVO<T> data(T data, String responseMessage) {
        return ResultVOBuilder.<T>aResult()
                .data(data)
                .code(0)
                .msg(responseMessage)
                .build();
    }

    // 带数据和消息的失败响应
    public static <T> ResultVO<T> fail(T data, String msg) {
        return ResultVOBuilder.<T>aResult()
                .data(data)
                .code(500)
                .msg(msg)
                .build();
    }

    // 仅带消息的失败响应
    public static <T> ResultVO<T> fail(String msg) {
        return ResultVOBuilder.<T>aResult()
                .data(null)
                .code(500)
                .msg(msg)
                .build();
    }

    // 使用响应枚举的失败响应
    public static <T> ResultVO<T> fail(ResponseEnum responseEnum) {
        return ResultVOBuilder.<T>aResult()
                .data(null)
                .code(responseEnum.getCode())
                .msg(responseEnum.getMessage())
                .build();
    }

    // 仅带数据的失败响应
    public static <T> ResultVO<T> fail(T data) {
        return ResultVOBuilder.<T>aResult()
                .data(data)
                .code(500)
                .msg(FAIL_CODE)
                .build();
    }

    // 构造器
    public ResultVO() {
    }

}
